package com.ruoyi.file.service;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.DeleteObjectsRequest;
import com.aliyun.oss.model.DeleteObjectsResult;
import com.aliyun.oss.model.VoidResult;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

@Service
public class OSSService {

    private static final Log logger = LogFactory.getLog(OSSService.class);
    @Value("${oss.endpoint}")
    private String endpoint;
    @Value("${oss.access_id}")
    private String accessKeyId;
    @Value("${oss.access_secret}")
    private String accessKeySecret;
    @Value("${oss.bucket_name}")
    private String bucketName;
    @Value("${oss.pathkey}")
    private String basePathKey;
    @Value("${oss.readpath}")
    private String readpath;

    /**
     * 上传Byte数组
     * @param pathkey 文件路径
     * @param data 文件的 byte数组
     * @return
     */
    public String ossByteUpload(String pathkey, byte[] data) {
        OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
        ossClient.putObject(bucketName, basePathKey + pathkey, new ByteArrayInputStream(data));
        ossClient.shutdown();
        return readpath + basePathKey + pathkey;
    }

    /**
     * 网络流上传
     * @param pathkey 文件地址
     * @param url  网络流地址
     * @return
     * @throws IOException
     */
    public String ossNetUpload(String pathkey, String url) throws IOException {
        OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
        InputStream inputStream = new URL(url).openStream();
        ossClient.putObject(bucketName, basePathKey + pathkey, inputStream);
        ossClient.shutdown();
        return basePathKey + pathkey;
    }

    /**
     * 删除单张图片
     * @param pathkey 图片完整路径
     * @return
     */
    public String ossSingleDelete(String pathkey){
        if (pathkey.contains(readpath)) {
            pathkey = pathkey.substring(readpath.length());
            OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
            VoidResult voidResult = ossClient.deleteObject(bucketName, pathkey);
            ossClient.shutdown();
            return String.valueOf(voidResult.getResponse().getStatusCode());
        }else {
            return null;
        }
    }

    /**
     * 批量删除多个文件
     */
    public String ossBatchDelete(List<String> pathKeys){
        ArrayList<String> list = new ArrayList<>();
        for (String pathKey : pathKeys) {
            if (pathKey.contains(readpath)) {
                list.add(pathKey.substring(readpath.length()));
            }
        }
        OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
        DeleteObjectsResult result = ossClient.deleteObjects(new DeleteObjectsRequest(bucketName).withKeys(list).withEncodingType("UTF-8"));
        List<String> deletedObjects = result.getDeletedObjects();
        ossClient.shutdown();
        return null;
    }



}
